<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>登录</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<style>
    body {
        margin: 0;
        padding: 0;
        background-image: url(./image/背景.jpg);
        background-size: 100%;
    }

    .box {
        width: 600px;
        height: 300px;
        background-color: rgb(251, 248, 248);

        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: calc(50% - 300px);

    }

    .box1 {
        width: 100%;
        height: 24px;
        text-align: center;
        margin-bottom: 40px;
    }
</style>
<!-- action="/login" method="post" enctype="application/x-www-form-urlencoded" -->
<body>
    <div class="box">
        <div class="box1">
            <h3>成都市龙泉驿区北泉路社区社区医院医生管理系统</h3>
        </div>
        <form class="form-horizontal"
            role="form">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input style="width: 400px" type="text" class="form-control" id="username" name="username"
                        placeholder="请输入名字">
                </div>
            </div>
            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input style="width: 400px;" type="password" class="form-control" id="password" name="password"
                        placeholder="请输入密码">
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button id="btnLogin" style="width: 400px;background-color: #28a745; margin-top: 40px;" type="button"
                        class="btn btn-default">登录</button>
                </div>
            </div>
        </form>

    </div>
    <script>
        // let btnLogin = $("#btnLogin")
        // btnLogin.on("click", function () {
        //     let password = $("#password").val();
        //     let username = $("#username").val();
        //     if (/^\w{6,20}$/.test(username) && /^\w{6,8}$/.test(password)) {
        //         $.ajax({
        //             type: "post",
        //             url: "/login",
        //             data: { "username": username, "password": password },
        //             success(data) {
        //                 console.log(data);
        //                 if (data.code === 0) {
        //                     let result = data.data[0]
        //                     sessionStorage.setItem('userInfo', JSON.stringify(result))
        //                     location.href = "./page/main.html"

        //                 }
        //             }
        //         })
        //     } else {
        //         alert('输入账号，密码格式不正确')
        //     }
        // })
        let btnLogin = document.getElementById( 'btnLogin' )
    
    btnLogin.onclick = function () {
      let username = document.getElementById( "username" ).value;
      let password = document.getElementById( "password" ).value;
      let xhr = new XMLHttpRequest() 
      xhr.open( 'post', '/login', true )  
      xhr.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" ) 
      xhr.send( `username=${ username }&password=${ password }` )
      xhr.onreadystatechange = function () {
        if ( xhr.readyState === 4 && xhr.status === 200 ) {
          let res = JSON.parse( xhr.responseText ) 
          if ( res.code === 0 ) {
            console.log( res );
            sessionStorage.setItem( 'userinfo', JSON.stringify( res.data[ 0 ] ) );
            location.href = "./page/main.html"
          } else {
            console.log( res );
          }
        }
      }
    }
       
</script>

</body>

</html>